package com.prs.crm.action.cdxh;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import com.prs.crm.annotation.Clew;
import com.prs.crm.domain.cdxh.SerialNumberss;
import com.prs.crm.domain.cdxh.production.MachineClass;
import com.prs.crm.domain.cdxh.production.ProvideRegime;
import com.prs.crm.service.cdxh.MachineClassService;
import com.prs.crm.service.cdxh.ProvideRegimeService;
import com.prs.crm.service.cdxh.SerialNumberssService;

/**
 * @author LanXiaoGang
 * @E-mail:lan9liang@163.com
 * @qq:7857553326
 * @version 创建时间：2010-12-3 上午10:10:52 简单说明
 */

public class MachineClassAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private MachineClass machineClass;

	private MachineClassService machineClassService;
	
	private SerialNumberssService serialNumberssService;

	private ProvideRegime provideRegime;

	private ProvideRegimeService provideRegimeService;

	private PaginationSupport pagination;
	/**
	 * 隐藏ID
	 */
	private String hiddenIds = "0,0";

	/**
	 * 生产类型
	 */
	private String productstype;

	/**
	 * 对应的各个单价
	 * 
	 * @return
	 */
	// 配料
	private int pl_activeoutput;
	private Double pl_activeoutput_jl;
	private Double pl_activeoutput_cf;
	private int pl_scrap;
	private Double pl_scrap_jl;
	private Double pl_scrap_cf;
	// 成型
	private int cx_activeoutput;
	private Double cx_activeoutput_jl;
	private Double cx_activeoutput_cf;
	private int cx_scrap;
	private Double cx_scrap_jl;
	private Double cx_scrap_cf;

	// 印刷
	private int ys_activeoutput;
	private Double ys_activeoutput_jl;
	private Double ys_activeoutput_cf;

	@Action("editMachineClass")
	public String editMachineClass() {

		return SUCCESS;
	}

	@Clew(results = { "success", "none", "error" })
	@Action(value = "saveMachineClass", results = {
			@Result(name = "success", type = "redirect", location = "editMachineClass"),
			@Result(name = "error", type = "redirect", location = "editMachineClass") })
	public String saveMachineClass() {
		if ("PL".equals(productstype)) {
			ProvideRegime prPL = this.getProvideRegimeService()
					.getReplaceProvideRegime("配料",
							machineClass.getMachineNum(),
							machineClass.getClassNum(), "实际产量");
			if (prPL != null) {
				this.setPrompt("对不起,该机号班次重复！");
				return ERROR;
			} else {
				this.getMachineClassService().savePL(productstype,
						machineClass, pl_activeoutput, pl_activeoutput_jl,
						pl_activeoutput_cf, pl_scrap, pl_scrap_jl, pl_scrap_cf);
			}
		} else if ("CX".equals(productstype)) {
			ProvideRegime prPL = this.getProvideRegimeService()
					.getReplaceProvideRegime("成型",
							machineClass.getMachineNum(),
							machineClass.getClassNum(), "废料");
			if (prPL != null) {
				this.setPrompt("对不起,该机号班次重复！");
				return ERROR;
			} else {
				this.getMachineClassService().saveCX(productstype,
						machineClass, cx_activeoutput, cx_activeoutput_jl,
						cx_activeoutput_cf, cx_scrap, cx_scrap_jl, cx_scrap_cf);
			}
		} else if ("YS".equals(productstype)) {
			ProvideRegime prPL = this.getProvideRegimeService()
					.getReplaceProvideRegime("印刷",
							machineClass.getMachineNum(),
							machineClass.getClassNum(), "实际生产量");
			if (prPL != null) {
				this.setPrompt("对不起,该机号班次重复！");
				return ERROR;
			} else {
				this.getMachineClassService().saveYS(productstype,
						machineClass, ys_activeoutput, ys_activeoutput_jl,
						ys_activeoutput_cf);
			}
		}

		this.setPrompt("新建机号班次成功");
		return SUCCESS;
	}

	@Action("listProvideRegime")
	public String listProvideRegime() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				ProvideRegime.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getProvideRegimeService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("viewPLMachineClass")
	public String viewPLMachineClass() {
		provideRegime = this.getProvideRegimeService().get(
				provideRegime.getId());
		/**
		 * 实际产量
		 */
		ProvideRegime prObject = this.getProvideRegimeService()
				.getProvideRegime("配料", "实际产量", provideRegime.getClassNums(),
						provideRegime.getMachineNums());
		this.pl_activeoutput = prObject.getSetNum();
		this.pl_activeoutput_jl = prObject.getRewardPrice();
		this.pl_activeoutput_cf = prObject.getPunishPrice();
		/**
		 * 废料
		 */

		ProvideRegime prObject1 = this.getProvideRegimeService()
				.getProvideRegime("配料", "废料", provideRegime.getClassNums(),
						provideRegime.getMachineNums());
		this.pl_scrap = prObject1.getSetNum();
		this.pl_scrap_jl = prObject1.getRewardPrice();
		this.pl_scrap_cf = prObject1.getPunishPrice();

		hiddenIds = prObject.getId() + "," + prObject1.getId();
		return SUCCESS;
	}

	@Action("viewCXMachineClass")
	public String viewCXMachineClass() {

		provideRegime = this.getProvideRegimeService().get(
				provideRegime.getId());
		/**
		 * 实际产量
		 */
		ProvideRegime prObject = this.getProvideRegimeService()
				.getProvideRegime("成型", "实际领料量", provideRegime.getClassNums(),
						provideRegime.getMachineNums());
		this.cx_activeoutput = prObject.getSetNum();
		this.cx_activeoutput_jl = prObject.getRewardPrice();
		this.cx_activeoutput_cf = prObject.getPunishPrice();

		/**
		 * 废料
		 */

		ProvideRegime prObject1 = this.getProvideRegimeService()
				.getProvideRegime("成型", "废料", provideRegime.getClassNums(),
						provideRegime.getMachineNums());
		this.cx_scrap = prObject1.getSetNum();
		this.cx_scrap_jl = prObject1.getRewardPrice();
		this.cx_scrap_cf = prObject1.getPunishPrice();

		hiddenIds = prObject.getId() + "," + prObject1.getId();
		return SUCCESS;
	}

	@Action("viewYSMachineClass")
	public String viewYSMachineClass() {
		provideRegime = this.getProvideRegimeService().get(
				provideRegime.getId());
		/**
		 * 实际产量
		 */
		ProvideRegime prObject = this.getProvideRegimeService()
				.getProvideRegime("印刷", "实际生产量", provideRegime.getClassNums(),
						provideRegime.getMachineNums());
		this.ys_activeoutput = prObject.getSetNum();
		this.ys_activeoutput_jl = prObject.getRewardPrice();
		this.ys_activeoutput_cf = prObject.getPunishPrice();

		hiddenIds = prObject.getId() + "";
		return SUCCESS;
	}

	@Clew(results = { "success", "none", "error" })
	@Action(value = "upPLMachaineClass", results = {
			@Result(name = "success", type = "redirect", location = "listProvideRegime"),
			@Result(name = "error", type = "redirect", location = "listProvideRegime") })
	public String upPLMachaineClass() {
		String ids[] = hiddenIds.split(",");
		ProvideRegime pr1 = this.getProvideRegimeService().get(
				Integer.parseInt(ids[0]));
		 String CodeNumbers=pr1.getDiscriminationValue();
		 String DF[]=CodeNumbers.split("-");
		 int lastCode=Integer.parseInt(DF[2])+1;
		 String lasd=DF[0]+"-"+DF[1]+"-"+lastCode;
		 pr1.setDiscriminationValue(lasd);
//		System.out.println(pr1);
		pr1.setSetNum(pl_activeoutput);
		pr1.setRewardPrice(pl_activeoutput_jl);
		pr1.setPunishPrice(pl_activeoutput_cf);
		this.getProvideRegimeService().setProvideRegime(pr1);
		ProvideRegime pr2 = this.getProvideRegimeService().get(
				Integer.parseInt(ids[1]));
		 String CodeNumbers1=pr2.getDiscriminationValue();
		 String DF1[]=CodeNumbers1.split("-");
		 int lastCode1=Integer.parseInt(DF1[2])+1;
		 String lasd1=DF1[0]+"-"+DF1[1]+"-"+lastCode1;
		 pr2.setDiscriminationValue(lasd1);
//		System.out.println(pr2);
		pr2.setSetNum(pl_scrap);
		pr2.setRewardPrice(pl_scrap_jl);
		pr2.setPunishPrice(pl_scrap_cf);
		this.getProvideRegimeService().setProvideRegime(pr2);

		this.setPrompt("配料数据修改成功");
		return SUCCESS;
	}

	@Clew(results = { "success", "none", "error" })
	@Action(value = "upCXMachaineClass", results = {
			@Result(name = "success", type = "redirect", location = "listProvideRegime"),
			@Result(name = "error", type = "redirect", location = "listProvideRegime") })
	public String upCXMachaineClass() {
		String ids[] = hiddenIds.split(",");
		ProvideRegime pr1 = this.getProvideRegimeService().get(
				Integer.parseInt(ids[0]));
		 String CodeNumbers=pr1.getDiscriminationValue();
		 String DF[]=CodeNumbers.split("-");
		 int lastCode=Integer.parseInt(DF[2])+1;
		 String lasd=DF[0]+"-"+DF[1]+"-"+lastCode;
		 pr1.setDiscriminationValue(lasd);
//		System.out.println(pr1);
		pr1.setSetNum(cx_activeoutput);
		pr1.setRewardPrice(cx_activeoutput_jl);
		pr1.setPunishPrice(cx_activeoutput_cf);
		this.getProvideRegimeService().setProvideRegime(pr1);
		ProvideRegime pr2 = this.getProvideRegimeService().get(
				Integer.parseInt(ids[1]));
		 String CodeNumbers1=pr2.getDiscriminationValue();
		 String DF1[]=CodeNumbers1.split("-");
		 int lastCode1=Integer.parseInt(DF1[2])+1;
		 String lasd1=DF1[0]+"-"+DF1[1]+"-"+lastCode1;
		 pr2.setDiscriminationValue(lasd1);
//		System.out.println(pr2);
		pr2.setSetNum(cx_scrap);
		pr2.setRewardPrice(cx_scrap_jl);
		pr2.setPunishPrice(cx_scrap_cf);
		this.getProvideRegimeService().setProvideRegime(pr2);
		this.setPrompt("成型数据修改成功");
		return SUCCESS;
	}

	@Clew(results = { "success", "none", "error" })
	@Action(value = "upYSMachaineClass", results = {
			@Result(name = "success", type = "redirect", location = "listProvideRegime"),
			@Result(name = "error", type = "redirect", location = "listProvideRegime") })
	public String upYSMachaineClass() {
		ProvideRegime pr1 = this.getProvideRegimeService().get(
				Integer.parseInt(hiddenIds));		
		 String CodeNumbers=pr1.getDiscriminationValue();
		 String DF[]=CodeNumbers.split("-");
		 int lastCode=Integer.parseInt(DF[2])+1;
		 String lasd=DF[0]+"-"+DF[1]+"-"+lastCode;
		 pr1.setDiscriminationValue(lasd);
		pr1.setSetNum(ys_activeoutput);
		pr1.setRewardPrice(ys_activeoutput_jl);
		pr1.setPunishPrice(ys_activeoutput_cf);
		
		this.getProvideRegimeService().setProvideRegime(pr1);

		this.setPrompt("印刷数据修改成功");
		return SUCCESS;
	}

	public MachineClass getMachineClass() {
		return machineClass;
	}

	public void setMachineClass(MachineClass machineClass) {
		this.machineClass = machineClass;
	}

	public MachineClassService getMachineClassService() {
		return machineClassService;
	}

	public void setMachineClassService(MachineClassService machineClassService) {
		this.machineClassService = machineClassService;
	}

	public ProvideRegime getProvideRegime() {
		return provideRegime;
	}

	public void setProvideRegime(ProvideRegime provideRegime) {
		this.provideRegime = provideRegime;
	}

	public ProvideRegimeService getProvideRegimeService() {
		return provideRegimeService;
	}

	public void setProvideRegimeService(
			ProvideRegimeService provideRegimeService) {
		this.provideRegimeService = provideRegimeService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public String getProductstype() {
		return productstype;
	}

	public void setProductstype(String productstype) {
		this.productstype = productstype;
	}

	public int getPl_activeoutput() {
		return pl_activeoutput;
	}

	public void setPl_activeoutput(int pl_activeoutput) {
		this.pl_activeoutput = pl_activeoutput;
	}

	public Double getPl_activeoutput_jl() {
		return pl_activeoutput_jl;
	}

	public void setPl_activeoutput_jl(Double pl_activeoutput_jl) {
		this.pl_activeoutput_jl = pl_activeoutput_jl;
	}

	public Double getPl_activeoutput_cf() {
		return pl_activeoutput_cf;
	}

	public void setPl_activeoutput_cf(Double pl_activeoutput_cf) {
		this.pl_activeoutput_cf = pl_activeoutput_cf;
	}

	public int getPl_scrap() {
		return pl_scrap;
	}

	public void setPl_scrap(int pl_scrap) {
		this.pl_scrap = pl_scrap;
	}

	public Double getPl_scrap_jl() {
		return pl_scrap_jl;
	}

	public void setPl_scrap_jl(Double pl_scrap_jl) {
		this.pl_scrap_jl = pl_scrap_jl;
	}

	public Double getPl_scrap_cf() {
		return pl_scrap_cf;
	}

	public void setPl_scrap_cf(Double pl_scrap_cf) {
		this.pl_scrap_cf = pl_scrap_cf;
	}

	public int getCx_activeoutput() {
		return cx_activeoutput;
	}

	public void setCx_activeoutput(int cx_activeoutput) {
		this.cx_activeoutput = cx_activeoutput;
	}

	public Double getCx_activeoutput_jl() {
		return cx_activeoutput_jl;
	}

	public void setCx_activeoutput_jl(Double cx_activeoutput_jl) {
		this.cx_activeoutput_jl = cx_activeoutput_jl;
	}

	public Double getCx_activeoutput_cf() {
		return cx_activeoutput_cf;
	}

	public void setCx_activeoutput_cf(Double cx_activeoutput_cf) {
		this.cx_activeoutput_cf = cx_activeoutput_cf;
	}

	public int getCx_scrap() {
		return cx_scrap;
	}

	public void setCx_scrap(int cx_scrap) {
		this.cx_scrap = cx_scrap;
	}

	public Double getCx_scrap_jl() {
		return cx_scrap_jl;
	}

	public void setCx_scrap_jl(Double cx_scrap_jl) {
		this.cx_scrap_jl = cx_scrap_jl;
	}

	public Double getCx_scrap_cf() {
		return cx_scrap_cf;
	}

	public void setCx_scrap_cf(Double cx_scrap_cf) {
		this.cx_scrap_cf = cx_scrap_cf;
	}

	public int getYs_activeoutput() {
		return ys_activeoutput;
	}

	public void setYs_activeoutput(int ys_activeoutput) {
		this.ys_activeoutput = ys_activeoutput;
	}

	public Double getYs_activeoutput_jl() {
		return ys_activeoutput_jl;
	}

	public void setYs_activeoutput_jl(Double ys_activeoutput_jl) {
		this.ys_activeoutput_jl = ys_activeoutput_jl;
	}

	public Double getYs_activeoutput_cf() {
		return ys_activeoutput_cf;
	}

	public void setYs_activeoutput_cf(Double ys_activeoutput_cf) {
		this.ys_activeoutput_cf = ys_activeoutput_cf;
	}

	public String getHiddenIds() {
		return hiddenIds;
	}

	public void setHiddenIds(String hiddenIds) {
		this.hiddenIds = hiddenIds;
	}

	public SerialNumberssService getSerialNumberssService() {
		return serialNumberssService;
	}

	public void setSerialNumberssService(SerialNumberssService serialNumberssService) {
		this.serialNumberssService = serialNumberssService;
	}

}
